package azcat.planner.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JComboBox;

import azcat.planner.materials.Material;
import azcat.planner.schedule.Schedule;
import azcat.planner.tools.Tool;
import azcat.planner.tools.ToolsManager;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dialog.ModalExclusionType;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

public class ToolsData extends JFrame {

	private JPanel contentPane;
	private JTabbedPane tabbedPane;
	private ArrayList<Material> neededMaterials;
	private JPanel buttonsPannel;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private ArrayList<String> selections = new ArrayList<String>();
	private ArrayList<Tool> neededTools;
	private ToolsManager manager;
	private MainWindow mw;

	public ArrayList<Tool> getNeededTools() {
		return neededTools;
	}

	/**
	 * Create the frame.
	 */
	public ToolsData(ArrayList<Material> neededMaterials, MainWindow mw1) {		
		this.mw = mw1;
		this.neededMaterials = neededMaterials;
		createGUI();
		generateTabs();		
	}		
	public void repaintGUI(ArrayList<Material> neededMaterials, MainWindow mw1){
		this.mw = mw1;
		this.neededMaterials = neededMaterials;
		createGUI();
		generateTabs();
	}
	private void createGUI(){
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		buttonsPannel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) buttonsPannel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		contentPane.add(buttonsPannel, BorderLayout.SOUTH);
		
		btnNewButton = new JButton("Aceptar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(getUserSelections()){
					createManager();
					manager.estimateSchedule();
					neededTools = manager.getNeededTools();//copy the needed tools to this class
					mw.setNeededTools(neededTools);
					mw.displayDates();
					mw.enableWorkersButton();
					dispose();
				}
				else{
					
				}
			}
		});
		buttonsPannel.add(btnNewButton);
		
		btnNewButton_1 = new JButton("Cancelar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		buttonsPannel.add(btnNewButton_1);
	}
	public ArrayList<Material> getMaterials(){
		return this.neededMaterials;
	}
	public ArrayList<String> getSelections() {
		return selections;
	}

	
	private void createManager(){
		manager = new ToolsManager(this);
	}
	
	private void generateTabs(){
		JPanel j;
		for(int i= 0; i< neededMaterials.size(); i++){//change for materials list size
			j = new ToolsLayoutPanel();			
			tabbedPane.addTab("Pieza "+ neededMaterials.get(i).getComponentName(), j);
			
		}
	}
	
	private boolean getUserSelections(){
		Component []tabs = tabbedPane.getComponents();
		selections.clear();
		for(int i =0; i< tabs.length; i++){//gets all the tabs
			Component []comps = ((Container)tabs[i]).getComponents();
			for(int j=0; j< comps.length; j++){//gets the comboboxes and textboxes for each tab
				if(comps[j].getClass() == JComboBox.class){
					JComboBox<String> box = (JComboBox<String>) comps[j];
					if(box.getSelectedItem().equals("")){
						checkComboBoxes();						
						box.requestFocus();
						return false;
					}
					else{
						selections.add((String) box.getSelectedItem());
						//System.out.println(box.getSelectedItem());//CHANGE
					}
				}
				else if(comps[j].getClass() == JTextField.class){					
					JTextField txt = (JTextField) comps[j];
					try{
						if(txt.getText().equals("") || Integer.parseInt(txt.getText()) <=0){
							checkTextBoxes();
							txt.requestFocus();
							return false;
						}
						selections.add(txt.getText());
					}catch(Exception e){
						checkTextBoxes();
						txt.requestFocus();
						return false; 
					}
					//System.out.println(txt.getText());//CHANGE
					
				}
			}
		}
		return true;
	}
	private void checkComboBoxes(){
		selections.clear();
		JOptionPane.showMessageDialog(this,
			    "Favor de Seleccionar Herramienta(s).",
			    "Advertencia",
			    JOptionPane.ERROR_MESSAGE);
	}
	
	private void checkTextBoxes(){
		selections.clear();
		JOptionPane.showMessageDialog(this,
			    "Valor Invalido.",
			    "Advertencia",
			    JOptionPane.ERROR_MESSAGE);
	}

}
